package BookStore.managers_impl.MinimumMarginMgr;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import Util.Util;

import BookStore.BookStoreMain;
import BookStore.managers.MinimumMarginMgr_Iface.MinimumMarginMgrPOA;

/**
 * Manager aiming to manage the bookstore minimum margin
 * @author Loup
 *
 */
public class MinimumMarginMgr extends MinimumMarginMgrPOA {
	private final String attr = "margin";

	@Override
	public boolean setMargin(float newMargin) {
		String file = BookStoreMain.prop;
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream(file));
			//update the minimum margin
			prop.setProperty(attr, newMargin + "");
			FileOutputStream fos = new FileOutputStream(file);
			prop.store(fos, null);

		} catch (Exception e) {
			System.out.println("File " + file + " cannot be found...");
			return false;
		}

		return true;
	}

	@Override
	public float getCurrentMargin() {
		//retrieve the current margin
		String m = Util.getPropertiesAttribute(BookStoreMain.prop, attr);
		if(m == null)
			return -1;
		return Float.parseFloat(m);

	}

}
